<!DOCTYPE html>
<html lang='zh-CN'>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <title>中奖奖品</title>
    <link rel="icon" href="favicon.ico" type="image/ico">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/materialdesignicons.min.css" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.min.css" rel="stylesheet">
    <link href="js/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://at.alicdn.com/t/font_1805932_ysrcp4y0uy9.css">
    <link rel="stylesheet" href="css/uploadImg.css">
</head>
<body>
<div class="container-fluid p-t-15">

    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header">

                    <div id="toolbar" class="toolbar-btn-action">
                        <form class="form-inline" method="post" id="searchForm">
                            <div class="form-group">
                                <button class="btn btn-success btn_query btn-round" type="button" id="addBtn">添加奖品</button>
                            </div>
                            <div class="form-group">
                                <button class="btn btn-warning btn_query btn-round" type="button" id="editBtn">编辑奖品</button>
                            </div>
                            <div class="form-group">
                                <button class="btn btn-purple btn_query btn-round" type="button" id="specifyBtn">指定中奖会员</button>
                            </div>
                            <div class="form-group">
                                <button class="btn btn-info btn_query btn-round" type="button" id="infoBtn">查看指定中奖人信息</button>
                            </div>
                        </form>
                    </div>

                </div>
                <div class="card-body">
                    <table id="grid_data" class="table text-nowrap"></table>
                </div>
            </div>
        </div>
    </div>

    <!--  添加与编辑模态框 -->
    <div class="modal fade" id="addOrEditModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <span>添加/编辑</span>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <div id="error_tips" style="color: #c9302c"></div>
                </div>
                <div class="modal-body">
                    <form id="addOrEditForm" method="post" enctype="multipart/form-data">
                        <div class="row">
                            <div class="col-xs-12">
                                <div class="input-group m-b-10">
                                    <span class="input-group-addon" id="basic-account">奖品名称</span>
                                    <input type="hidden" name="addOrEditStatus" VALUE="0" id="addOrEditStatus" placeholder="添加或编辑图片状态控制">
                                    <input type="hidden" name="id">
                                    <input type="hidden" name="logo">
                                    <input type="hidden" name="deleteUrl">
                                    <input type="text" name="prizeName" class="form-control">
                                    <span class="mdi mdi-account form-control-feedback" aria-hidden="true"></span>
                                </div>
                            </div>

                            <div class="col-xs-12">
                                <div class="input-group m-b-10">
                                    <span class="input-group-addon">奖品价格</span>
                                    <input type="text" name="prizePrice" class="form-control" onkeyup="value=value.replace(/[^\d]/g,'')">
                                </div>
                            </div>

                            <div class="col-xs-12">
                                <div class="input-group m-b-10">
                                    <span class="input-group-addon">奖品数量</span>
                                    <input type="text" name="amount" class="form-control" onkeyup="value=value.replace(/[^\d]/g,'').replace(/^[0]+[0-9]*$/gi,'')">
                                </div>
                            </div>

                            <div class="col-xs-12">
                                <div class="input-group m-b-10">
                                    <span class="input-group-addon">奖品等级</span>
                                    <input type="text" name="prizeLevel" class="form-control" onkeyup="value=value.replace(/[^\d]/g,'')">
                                </div>
                            </div>

                            <div class="col-xs-12">
                                <div class="main u-main">
                                    <span class="label-left">上传图片</span>
                                    <div class="upload-content">
                                        <div class="content-img">
                                            <ul class="content-img-list"></ul>
                                            <div class="file">
                                                <i class="gcl gcladd"></i>
                                                <input type="file" name="file" accept="image/*" id="upload">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                            <button type="button" class="btn btn-primary addSaveBtn" id="saveBtn">保存</button>
                        </div>
                    </form>
                </div>

            </div>
        </div>
    </div>

    <!-- 指定中奖会员 -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">指定中奖会员</h4>
                </div>
                <div class="modal-body">
                    <form class="form-inline" method="post" id="searchCouponForm">
                        <div class="form-group">
                            <label>会员账号:</label>
                            <input type="text" class="form-control" name="memberAccount">
                           </div>
                        <div class="form-group">
                            <button class="btn btn-info btn_query btn-round" type="button" id="searchFormBtn">查询</button>
                        </div>
                    </form>
                    <div class="tab-content p-10">
                        <table id="member_data" class="table text-nowrap"></table>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="fixBtn">确定</button>
                </div>
            </div>
        </div>
    </div>

    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/bootstrap-table/bootstrap-table.js"></script>
    <script type="text/javascript" src="js/bootstrap-table/bootstrap-table-zh-CN.min.js"></script>
    <script type="text/javascript" src="js/zero2one/zero2one.version.js"></script>
    <script type="text/javascript" src="js/zero2one/zero2one.core.js"></script>
    <!-- bootstrap表单验证插件 -->
    <script type="text/javascript" src="js/bootstrapvalidator/dist/js/bootstrapValidator.js"></script>
    <!-- bootstrap弹出框插件 -->
    <script type="text/javascript" src="js/jquery.bootstrap.min.js"></script>
    <!-- 表单填充插件 -->
    <script type="text/javascript" src="js/jquery-form/jquery.formloader.js"></script>
    <script src="js/uploadImg.js"></script>
    <script type="text/javascript" src="js/zero2one/zero2one.curd.js"></script>
    <script type="text/javascript">

        //设置表头
        var columnss = [
            {checkbox: true},
            {field: 'memberId',title: '会员ID'},
            {field: 'memberAccount',title: '会员账号'},
            {field: 'nickName',title: '会员昵称'},
            {field: 'memberPhone',title: '手机号码'}
        ];

        //进入主界面加载数据
        zero2one.datagrids("member_data", "/remote/member/list",function (params) {
            return $.extend(zero2one.tableInitParam(params), zero2one.form2json("searchCouponForm"));
        }, columnss,function (data) {});

        //点击查询按钮重新加载页面数据
        $("#searchFormBtn").click(function () {
            $('#member_data').bootstrapTable('refresh');
        });

        //设置表头
        var columns = [
            {checkbox: true},
            {field: 'id',title: '编号'},
            {field: 'prizeName',title: '奖品名称'},
            {field: 'prizePrice',title: '奖品价格'},
            {field: 'prizeLevel',title: '奖品等级'},
            {field: 'logo',title: '奖品图片',formatter: function (value, row, index) {
                    return value != null ? "<img style='width: 50px;height: 50px;' src='"+value+"'/>" : "";
                }},
            {field: 'amount',title: '奖品数量'}
        ];

        //加载列表信息
        zero2one.table("/remote/prizeInfo/list", columns);

        //点击编辑按钮弹出模态框
        $("#editBtn").click(function () {
            var datas = $('#grid_data').bootstrapTable('getSelections');
            if(datas.length == 1){
                $("#addOrEditForm").formloader({data: datas[0]});
                $("#addOrEditStatus").val(1);
                if(datas[0].logo.length > 0){ addNewContent(".content-img-list", datas[0].logo.split(",")); }
                $("#addOrEditModal").modal("show");
            }else{
                $.messager.alert("温馨提示", "请选择一条数据操作");
            }
        });

        //点击添加按钮弹出模态框
        $("#addBtn").click(function () {
            $("#addOrEditForm")[0].reset();
            $("input[type='hidden']").val("");//清空hidden值
            $(".content-img-list").html("");//清空图片回显信息
            $("#addOrEditModal").modal("show");
        });

        //选择图片还没有上传
        var imgFile = uploadImg(".content-img-list", "upload", 1024 * 1024 * 4, 0);

        //点击保存按钮提交请求且提交前先执行表单验证
        $('#addOrEditForm').bootstrapValidator({
            fields: {
                prizeName: {validators: {notEmpty: {message: '奖品名称不能为空'}}},
                logo: {validators: {notEmpty: {message: '奖品图片不能为空'}}},
                prizeLevel: {validators: {notEmpty: {message: '奖品等级不能为空'}}},
                amount: {validators: {notEmpty: {message: '奖品数量不能为空'}}}
            }
        });

        //添加编辑操作
        $("#saveBtn").click(function () {
            var bv = $('#addOrEditForm').data('bootstrapValidator');
            bv.validate();
            if (bv.isValid()) {
                var formFile = new FormData();
                // 遍历图片imgFile添加到formFile里面
                $.each(imgFile,function (i, v) {
                    formFile.append("file", v)
                });
                handleImage('logo', 'deleteUrl');//编辑处理删除的图片
                zero2one.requestU("addOrEditForm","/remote/prizeInfo/addOrEdit",formFile,function (data) {
                    if (data.success) {
                        $.messager.alert("温馨提示", "操作成功");
                        window.location.reload();
                    } else { $.messager.alert("温馨提示", data.msg) }
                })
            }
        });

        //选择奖品指定中奖会员
        $("#specifyBtn").click(function () {
            var datas = $("#grid_data").bootstrapTable('getSelections');
            if(datas.length == 1){
                $("#myModal").modal("show");
            }else {
                $.messager.alert("温馨提示", "请选择一个奖品操作");
            }
        });

        $("#fixBtn").click(function () {
            var datas = $("#grid_data").bootstrapTable('getSelections');
            var dataes = $("#member_data").bootstrapTable('getSelections');
            if(dataes.length == 1){
                var vo={prizeId:datas[0].id, memberId:dataes[0].memberId};
                $.ajax({ url: '/remote/prizeWin/addOrEdit', contentType:'application/json', type: 'post', data: JSON.stringify(vo), dataType: 'json', success: function (res) {
                        if (res.success) {
                            $.messager.alert("温馨提示", "操作成功,2S之后刷新");
                            setTimeout(function () { window.location.reload(); }, 2000);
                        } else { $.messager.alert("温馨提示", res.msg) }
                    }
                });
            }else {
                $.messager.alert("温馨提示", "请选择一名会员操作");
            }
        });

        $("#infoBtn").click(function () {
            zero2one.tab("指定中奖信息","/toView?url=service/market/prizeWinMapping");
        });

    </script>
</div>
</body>
</html>
